package com.itheima.Controller;

import com.itheima.Service.ProductService;
import com.itheima.pojo.Category;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.ApplicationScope;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/api/categories")
public class CategoryController {
    @Autowired
    private ProductService productService;

    /*
     *   查询所有商品的种类
     * */
    @GetMapping
    private Result categoriesList(@RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                  String name) {
        log.info("查询所有商品种,{},{},{}",page,pageSize,name);
        PageBean pageBean = productService.categoriesList(page,pageSize,name);
        return Result.success(pageBean);
    }

    /*
     *   根据id删除商品类别
     * */
    @DeleteMapping("/{ids}")
    private Result delete(@PathVariable List<Integer> ids) {
        log.info("调用删除商品种类,{}",ids);
        productService.deleteCategoryById(ids);
        return Result.success();
    }

    /*
     *   修改商品分类
     * */
    @PutMapping
    private Result update(@RequestBody Category category) {
        log.info("调用修改商品种类接口,{}", category);
        productService.updateCategory(category);
        return Result.success();
    }

    /*
     *   新增商品分类
     * */
    @PostMapping
    private Result add(@RequestBody Category category) {
        log.info("调用新增商品种类接口,{}", category);
        productService.insertCategory(category);
        return Result.success();
    }

    /*
     *   根据id查询商品分类
     * */
    @GetMapping("/{id}")
    private Result select(@PathVariable int id) {
        log.info("调用查询商品种类接口,{}",id);
        Category category = productService.selectCategoryById(id);
        return Result.success(category);
    }
}
